home *** CD-ROM | disk | FTP | other *** search
/ Robotics & Artificial Int…3 (Professional Edition) / Robotics & Artificial Intelligence Tools 2003 (Professional Edition).iso / neural network tool and application / nsinstall.exe / data1.cab / CustomSolutionWizard_Files / ProjectShells / VBShell.vb < prev    next >
Encoding:
Text File  |  2002-03-08  |  13.0 KB  |  249 lines

  1. Option Strict Off
  2. Option Explicit On 
  3. Module NNTrainAndGetResponse
  4.  
  5.     Public resetNetwork As Boolean
  6.  
  7.     Function TrainNetwork() As Object
  8.  
  9.         'DESCRIPTION: This subroutine illustrates how the DLL generated by the Custom
  10.         'Solution Wizard can be used for training in a Visual Basic application.  The
  11.         'subroutine trains the network DLL saving the best weights to a specified file for
  12.         'later use.  Step by step instructions are given on how to use the generated DLL
  13.         'for this purpose.  Read through comments carefully considering all of the IMPLEMENTATION
  14.         'NOTES as you get to them.
  15.  
  16.         'NOTE: This subroutine only applies to users of the Developers level of the Custom
  17.         'Solution Wizard.  The Developers level allows the user to generate learning DLLs,
  18.         'whereas all other levels can only generate recall DLLs.
  19.  
  20.         'Step 1: Create a new neural network object of the NSLearningNetwork type.
  21.         '-------------------------------------------------------------------------
  22.         Dim nn As New NeuroSolutions.NSLearningNetwork()
  23.  
  24.         'Step 2: Set the pathName of the generated network DLL.
  25.         '------------------------------------------------------
  26.         'IMPLEMENTATION NOTES: The DLL_PATH_NAME below is defined in the Globals module as the
  27.         'path to the newly generated DLL.
  28.         'MORE INFO: The call to dllPathName will fail if you do not have the Developers
  29.         'level of the Custom Solution Wizard or if you generated the DLL using a recall
  30.         'NeuroSolutions  breadboard.  If either of these cases apply, you will only be able
  31.         'to create NSRecallNetwork objects (which are not trainable).
  32.         nn.dllPathName = DLL_PATH_NAME
  33.  
  34.         'Step 3: Load the initial network weights. (Optional)
  35.         '----------------------------------------------------
  36.         'IMPLEMENTATION NOTES: The WEIGHTS_PATH_NAME below is defined in the Globals module as
  37.         'the path to the weights file which was saved when the DLL was generated.
  38.         'MORE INFO: This step is not necessary.  If this step is not performed, the
  39.         'network will simply start with random initial weights.
  40.         nn.loadWeights(WEIGHTS_PATH_NAME)
  41.  
  42.         'Resets the network (randomizes the network weights, etc.) if the user has so indicated
  43.         'via the "Reset Network before Training" check box on the VBShellDlg form
  44.         If resetNetwork = True Then
  45.             nn.resetNetwork()
  46.         End If
  47.  
  48.         'Step 4: Define the input data.
  49.         '------------------------------
  50.         'IMPLEMENTATION NOTES: The following loads the training input data from your initial
  51.         'network configuration into the array inputData.  The training input data was saved
  52.         'to a file during DLL generation.  The location of this file is specified in
  53.         'the Globals module.  There are a number of ways to load your inputData, including
  54.         'hard-coding, user input, and file access.  This implementation reads the data from
  55.         'a text file using DAO (other file types supported by DAO can also be used - see your
  56.         'Visual Basic documentation or consult the Microsoft Developer Network for more
  57.         'information on DAO).  This approach maximizes flexibility for both data access and
  58.         'manipulation.
  59.         'MORE INFO: Note that the number of inputs in the input data must match the number of inputs
  60.         'expected by the generated DLL.  Also, there can be any number of exemplars in the
  61.         'input data as long as the number of exemplars in the input data matches the number
  62.         'of exemplars in the desired data.
  63.         Dim inputData As Object
  64.         inputData = createDataArrayFromFile(DATA_PATH, INPUT_FILE_NAME)
  65.  
  66.         'Step 5: Send the input data to the network DLL.
  67.         '-----------------------------------------------
  68.         nn.inputData = inputData
  69.  
  70.         'Step 6: Define the desired data.
  71.         '--------------------------------
  72.         'IMPLEMENTATION NOTES: The following loads the training desired data from your initial
  73.         'network configuration into the array desiredData.  The training desired data was saved
  74.         'to a file during DLL generation.  The location of this file is specified in
  75.         'the Globals module.  There are a number of ways to load your desiredData, including
  76.         'hard-coding, user input, and file access.  This implementation reads the data from
  77.         'a text file using DAO (other file types supported by DAO can also be used - see your
  78.         'Visual Basic documentation or consult the Microsoft Developer Network for more
  79.         'information on DAO).  This approach maximizes flexibility for both data access and
  80.         'manipulation.
  81.         'MORE INFO: Note that the number of outputs in the desired data must match the number of outputs
  82.         'expected by the generated DLL.  Also, there can be any number of exemplars in the
  83.         'desired data as long as the number of exemplars in the desired data matches the number
  84.         'of exemplars in the input data.
  85.         Dim desiredData As Object
  86.         desiredData = createDataArrayFromFile(DATA_PATH, DESIRED_FILE_NAME)
  87.  
  88.         'Step 7: Send the desired data to the network DLL.
  89.         '-------------------------------------------------
  90.         nn.desiredData = desiredData
  91.  
  92.         'Step 8: Enable the automatic saving of the best weights. (Optional)
  93.         '-------------------------------------------------------------------
  94.         'MORE INFO: This step is not necessary.  The default value of this property is
  95.         'always False.  Setting it to True will cause the best network weights to be saved
  96.         'during training.  If this property is set to True, be sure to set the
  97.         'bestWeightsPathName to a valid pathName (See Step 9).
  98.         nn.saveBestWeightsEnabled = True
  99.  
  100.         'Step 9: Set the pathName for saving the best weights. (Optional)
  101.         '----------------------------------------------------------------
  102.         'IMPLEMENTATION NOTES: The BEST_WEIGHTS_PATH_NAME below is defined in the
  103.         'Globals module.
  104.         'MORE INFO: The specified pathName will be used for saving the best weights during
  105.         'training.  This bestWeightsPathName property must be set to a valid pathName
  106.         'if saveBestWeightsEnabled has been set to True.  Otherwise, setting this property
  107.         'is optional.
  108.         nn.bestWeightsPathName = BEST_WEIGHTS_PATH_NAME
  109.  
  110.         'Step 10: Train the network.
  111.         '---------------------------
  112.         'IMPLEMENTATION NOTES: The NUMBER_OF_EPOCHS is determined from the initial network configuration
  113.         'and is specified in the Globals module.
  114.         nn.train(NUMBER_OF_EPOCHS)
  115.  
  116.         'Step 11: Get the best cost achieved during training.
  117.         '----------------------------------------------------
  118.         Dim bestCost As Single
  119.         bestCost = nn.bestCost
  120.         TrainNetwork = bestCost
  121.  
  122.         'Step 12: Release the neural network object.
  123.         '-------------------------------------------
  124.         nn = Nothing
  125.  
  126.     End Function
  127.  
  128.     Function GetNetworkResponse() As Object
  129.  
  130.         'DESCRIPTION: This function illustrates how the DLL generated by the Custom
  131.         'Solution Wizard can be used for getting the network response (output) in a Visual
  132.         'Basic application.  Step by step instructions are given on how to use the
  133.         'generated DLL for this purpose.  Read through comments carefully considering all
  134.         'of the IMPLEMENTATION NOTES as you get to them.  This function returns the network
  135.         'response.
  136.  
  137.         'NOTE: This subroutine applies to users of any level of the Custom Solution Wizard.
  138.  
  139.         'Step 1: Create a new neural network object of the NSRecallNetwork type.
  140.         '-----------------------------------------------------------------------
  141.         'MORE INFO: You could create an NSLearningNetwork object network instead, if you
  142.         'have the Developers level of the Custom Solution Wizard and used it along with a
  143.         'NeuroSolutions breadboard capable of learning to generate the DLL.
  144.         Dim nn As New NeuroSolutions.NSRecallNetwork()
  145.  
  146.         'Step 2: Set the pathName of the generated network DLL.
  147.         '------------------------------------------------------
  148.         'IMPLEMENTATION NOTES: The DLL_PATH_NAME below is defined in the Globals module as the
  149.         'path to the newly generated DLL.
  150.         nn.dllPathName = DLL_PATH_NAME
  151.  
  152.         'Step 3: Load the saved network weights.
  153.         '---------------------------------------
  154.         'IMPLEMENTATION NOTES: The BEST_WEIGHTS_PATH_NAME below is defined in the Globals module.
  155.         'The initial best weights file is an exact copy of the weights file that was saved
  156.         'when the DLL was generated. However, the best weights file will change with each
  157.         'run of the TrainNetwork function if the network is reset before training.
  158.         nn.loadWeights(BEST_WEIGHTS_PATH_NAME)
  159.  
  160.         'Step 4: Define the input data.
  161.         '------------------------------
  162.         'IMPLEMENTATION NOTES: The following loads the training input data from your initial
  163.         'network configuration into the array inputData.  The training input data was saved
  164.         'to a file during DLL generation.  The location of this file is specified in
  165.         'the Globals module.  There are a number of ways to load your inputData, including
  166.         'hard-coding, user input, and file access.  This implementation reads the data from
  167.         'a text file using DAO (other file types supported by DAO can also be used - see your
  168.         'Visual Basic documentation or consult the Microsoft Developer Network for more
  169.         'information on DAO).  This approach maximizes flexibility for both data access and
  170.         'manipulation.
  171.         'MORE INFO: Note that the number of inputs in the input data must match the number of inputs
  172.         'expected by the generated DLL.
  173.         Dim inputData As Object
  174.         inputData = createDataArrayFromFile(DATA_PATH, INPUT_FILE_NAME)
  175.  
  176.         'Step 5: Send the input data to the network DLL.
  177.         '-----------------------------------------------
  178.         nn.inputData = inputData
  179.  
  180.         'Step 6: Get the network response (output).
  181.         '------------------------------------------
  182.         'MORE INFO: The network response is assigned to the return value of GetNetworkResponse
  183.         'function.
  184.         GetNetworkResponse = nn.getResponse
  185.  
  186.         'Step 7: Release the neural network object.
  187.         '------------------------------------------
  188.         nn = Nothing
  189.  
  190.     End Function
  191.  
  192.     Function createDataArrayFromFile(ByRef filePath As String, ByRef fileName As String) As Object
  193.  
  194.         'The following describes general information about the use of this function.
  195.         'Usually, no further action is required. However, this information is
  196.         'provided to explain how use this function on a different computer or with
  197.         'a different dataset.
  198.  
  199.         'In order to use this function, you must have Microsoft DAO 3.6 / Jet 4.0 installed
  200.         'on your machine (installed during the Custom Solution Wizard installation if
  201.         'not already present).
  202.  
  203.         'You must also have a reference to the Microsoft DAO 3.6 Object Library.
  204.         'To add this reference, within Visual Basic, goto to Project menu, click
  205.         'References, then place a check next to the Microsoft DAO 3.6 Object Library
  206.         'item in the resulting list box. Note: This reference has already been added for
  207.         'this project.
  208.  
  209.         'Furthermore, use of this function requires that you have created a schema.ini
  210.         'file defining the format of your data file and placed this file in the same
  211.         'directory as your data file (already done for the training data taken from
  212.         'the breadboard used to create the DLL).
  213.  
  214.         'Consider for example the following tab delimited XOr input data:
  215.         'x   y
  216.         '0   0
  217.         '0   1
  218.         '1   0
  219.         '1   1
  220.  
  221.         'If this XOr input data were placed within a text file named "XOr.txt", a "schema.ini"
  222.         'file (placed in the same directory as the "XOr.txt" file) with the following
  223.         'information could be used to correctly read this file:
  224.  
  225.         '[XOr.txt]
  226.         'ColNameHeader = True
  227.         'Format = TabDelimited
  228.         'MaxScanRows = 0
  229.         'CharacterSet = ANSI
  230.  
  231.         'For more information on using schema.ini files see the Reading Data from Text
  232.         'Files topic in the Custom Solution Wizard help.
  233.         Dim DAODBEngine As New DAO.DBEngine()
  234.         Dim dbs As DAO.Database
  235.         dbs = DAODBEngine.OpenDatabase(filePath, False, True, "Text;")
  236.  
  237.         Dim rst As DAO.Recordset
  238.         rst = dbs.OpenRecordset(fileName, DAO.RecordsetTypeEnum.dbOpenSnapshot)
  239.         rst.MoveLast()
  240.         Dim numberOfRecords As Integer
  241.         numberOfRecords = rst.RecordCount
  242.         rst.MoveFirst()
  243.         createDataArrayFromFile = rst.GetRows(numberOfRecords)
  244.  
  245.         rst.Close()
  246.         dbs.Close()
  247.  
  248.     End Function
  249. End Module